﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Eis51.CampanyCMS.Business.Bus;

using Eis51.Web.Admin;

namespace Eis51.CampanyCMS.WebUI.Admin.Coupon
{
    public partial class CouponList : BasePage
    {
        protected void Page_Load(object sender, System.EventArgs e)
        {
            // 在此处放置用户代码以初始化页面
            if (!IsPostBack)
            {
                btnSearch_Click(null, null);
            }
        }


        /// <summary>
        /// 查找
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSearch_Click(object sender, System.EventArgs e)
        {
            //int isdo = int.Parse(drpDo.SelectedValue);

            DataTable dt = CouponBLL.GetInstance().Search(dateBegindate.Text, dateEnd.Text, drpstatus.SelectedValue,txtCoupon.Text);
            dgCoupon.Data = dt;
            dgCoupon.DataBind();

        }



        protected void dgCoupon_ItemCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            switch (e.CommandName)
            {
                case "Delete":
                     CouponBLL.GetInstance().Remove(int.Parse(e.Item.Cells[0].Text));
                    btnSearch_Click(null, null);
                    break;
                case "FPage":
                    Eis51.CampanyCMS.Model.Bus.Coupon  c = CouponBLL.GetInstance().Get(new Eis51.CampanyCMS.Model.Bus.Coupon.Key(int.Parse(e.Item.Cells[0].Text)));
                    if (c.Status == 1)
                    {
                        c.Status = 0;
                    }
                    else
                    {
                        c.Status = 1;
                    }
                    CouponBLL.GetInstance().Update(c);
                    btnSearch_Click(null, null);
                    break;
                 
                default:
                    string url = "CouponPage.aspx?CouponId=" + e.Item.Cells[0].Text;
                    Response.Redirect(url);
                    break;
            }
        }

        protected void dgCoupon_ItemDataBound(object sender, System.Web.UI.WebControls.DataGridItemEventArgs e)
        {

            
            if (e.Item.ItemType == ListItemType.AlternatingItem ||
                e.Item.ItemType == ListItemType.Item)
            {
                LinkButton btnDelete = e.Item.FindControl("btnDelete") as LinkButton;
                if (btnDelete != null)
                {
                    btnDelete.Attributes.Add("onclick", "javascript:return confirm('确定要删除吗?');");
                }
                LinkButton btnIsFst = e.Item.FindControl("btnIsFst") as LinkButton;
                if (btnIsFst != null)
                {
                    if (e.Item.Cells[1].Text == "1")
                    {
                        btnIsFst.Text = "是";
                    }
                    else
                    {
                        btnIsFst.Text = "否";
                    }
                   
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            Response.Redirect("CouponPage.aspx");
        }

        
    }
}
